export interface IR {
    type: 'eok_ir';
    edition: number;
    comment: string[];
    element: string[];
    [key: string]: {}[] | number | string;
}

export const ply_resolver = (raw: string): IR => {
    const flow = raw.split('\n')
    console.log(flow)
    let i = 0
    type Property = {
        name: string,
        type: 'list<number>' | 'list<string>' | 'number' | 'string'
    }
    type Element = {
        name: string,
        num: number,
        property: Property[]
    }
    let body_content: Element[] = []
    let res: IR = {
        type: 'eok_ir',
        edition: 1,
        comment: [],
        element: []
    }

    // header
    while (flow[i] !== 'end_header') {
        console.log(i)
        if (flow[i].startsWith('comment')) {
            res.comment.push(flow[i].replace(/comment /, ''))
        } else if (flow[i].startsWith('element')) {
            const element: Element = {
                name: flow[i].replace(/element | \d*/g, ''),
                num: Number(flow[i].replace(/[^\d]*/g, '')),
                property: []
            }
            i++
            while (flow[i].startsWith('property')) {
                const tokens = flow[i].split(' ')
                const property: Property = {
                    name: '',
                    type: 'string'
                }
                if (tokens[1] === 'list') {
                    property.type = tokens[3] === 'string' ? 'list<string>' : 'list<number>'
                    property.name = tokens[4]
                } else {
                    property.type = tokens[1] === 'string' ? 'string' : 'number'
                    property.name = tokens[2]
                }
                element.property.push(property)
                i++
            }
            body_content.push(element)
            i--
        }
        i++
    }
    i++
    console.log(body_content)

    // body
    for (const element of body_content) {
        res.element.push(element.name);
        (res as any)[element.name] = []
        for (let j = 0; j < element.num; j++) {
            const tokens = flow[i + j].trim().split(' ')
            let k = 0
            const row = {}
            for (const property of element.property) {
                if (property.type === 'string') {
                    (row as any)[property.name] = tokens[k]
                } else if (property.type === 'number') {
                    (row as any)[property.name] = Number(tokens[k])
                } else if (property.type.startsWith('list')) {
                    (row as any)[property.name] = tokens.reduce<(Number | string)[]>((res, cur, index) => {
                        if (index !== 0) {
                            res.push(/number/.test(property.type) ? Number(cur) : cur)
                        }
                        return res
                    }, [])
                }
                k++
            }
            (res as any)[element.name].push(row)
        }
        i = i + element.num
    }

    return res
}